{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[None, None, 3, 4, 5]\n",
      "[6, 7, 3, 4, 5]\n",
      "[3, 4, 5, 6, 7, 8, None, None, None, None]\n"
     ]
    }
   ],
   "source": [
    "class ArrayQueue(object):\n",
    "    DEFAULT_CAPACITY = 5\n",
    "    def __init__(self):\n",
    "        self._data = [None] * ArrayQueue.DEFAULT_CAPACITY\n",
    "        self._size = 0\n",
    "        self._front = 0\n",
    "        \n",
    "    def __len__(self) :\n",
    "        return self._size\n",
    "    \n",
    "    def is_empty(self) :\n",
    "        return self._size == 0\n",
    "    \n",
    "    def first(self) :\n",
    "        if self.is_empty() :\n",
    "            raise Empty('Queue is empty')\n",
    "        return self._data[self._front]\n",
    "    \n",
    "    def dequeue(self) :\n",
    "        if self.is_empty() :\n",
    "            raise Empty('Queue is empty')\n",
    "        result = self._data[self._front]\n",
    "        self._data[self._front]=None\n",
    "        self._front = (self._front + 1) % len(self._data)\n",
    "        self._size -= 1\n",
    "        return result\n",
    "    \n",
    "    def enqueue(self,e) :\n",
    "        if self._size == len(self._data) :\n",
    "            self._resize(2 * len(self._data))\n",
    "        avail = ( self._front + self._size ) % len(self._data)\n",
    "        self._data[avail] = e\n",
    "        self._size += 1\n",
    "        \n",
    "    def _resize(self,cap) :\n",
    "        old = self._data\n",
    "        self._data = [None] * cap\n",
    "        walk = self._front\n",
    "        for k in range(self._size) :\n",
    "            self._data[k] = old[walk]\n",
    "            walk = (1 + walk ) % len(old)\n",
    "        self._front = 0\n",
    "\n",
    "    def __repr__(self) :\n",
    "        data = []\n",
    "        walk = self._front\n",
    "        for k in range(self._size) :\n",
    "            data.append(self._data[walk])\n",
    "            walk = (1 + walk ) % len(self._data)\n",
    "        return f'{ data }'\n",
    "\n",
    "if __name__ =='__main__':\n",
    "    Q = ArrayQueue ()\n",
    "    Q.enqueue(1)\n",
    "    Q.enqueue(2)\n",
    "    Q.enqueue(3)\n",
    "    Q.enqueue(4)\n",
    "    Q.enqueue(5)\n",
    "    Q.dequeue()\n",
    "    Q.dequeue()\n",
    "    print(Q._data)\n",
    "    Q.enqueue(6)\n",
    "    Q.enqueue(7)\n",
    "    print(Q._data)\n",
    "    Q.enqueue(8)\n",
    "    print(Q._data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
